package com.ruoyi.web.controller.kaoyan;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kaoyan.domain.EducationCategory;
import com.ruoyi.kaoyan.domain.EducationForumComment;
import com.ruoyi.kaoyan.service.EducationCategoryService;
import com.ruoyi.kaoyan.service.EducationForumCommentService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.controller.kaoyan.vo.CommentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 后台评论管理
 * @author admin
 */
@Api(tags = "后台评论管理审核", value = "后台评论管理审核")
@RestController
@RequestMapping("/education/comment")
public class EducationCommentController extends BaseController {

    @Autowired
    private EducationForumCommentService commentService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private EducationCategoryService categoryService;

    @ApiOperation("评论列表")
    @PostMapping("/list")
    public AjaxResult commentList(String userName, Integer verFlag){
        LambdaQueryWrapper<EducationForumComment> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(userName)){
            SysUser user = sysUserService.getOne(new LambdaQueryWrapper<SysUser>().eq(true, SysUser::getUserName, userName));
            if (!StringUtils.checkObjAllFieldsIsNull(user)){
                Long userId = user.getUserId();
                queryWrapper.eq(true, EducationForumComment::getUserId, userId);
            }
        }
        if (StringUtils.isNull(verFlag)){
            queryWrapper.isNull(EducationForumComment::getVerFlag);
        }else{
            queryWrapper.eq(EducationForumComment::getVerFlag, verFlag);
        }
        Page<EducationForumComment> page = commentService.page(startPagePlus(), queryWrapper);
        List<EducationForumComment> commentList = page.getRecords();
        Set<Long> userIdSet = commentList.stream().map(EducationForumComment::getUserId).collect(Collectors.toSet());
        Set<Long> categoryIdSet = commentList.stream().map(EducationForumComment::getCategoryId).collect(Collectors.toSet());
        categoryIdSet.add(0L);
        Map<Long, String> map = categoryService.listByIds(categoryIdSet).stream().collect(Collectors.toMap(EducationCategory::getId, EducationCategory::getName));
        List<SysUser> sysUsers = sysUserService.list(new LambdaQueryWrapper<SysUser>().in(StringUtils.isNotEmpty(userIdSet), SysUser::getUserId, userIdSet));
        Map<Long, String> userNameMap = sysUsers.stream().collect(Collectors.toMap(SysUser::getUserId, SysUser::getUserName));
        List<CommentVo> commentVoList = commentList.stream().map(e -> new CommentVo(e, map, userNameMap)).collect(Collectors.toList());
        return AjaxResult.ok().page(page, commentVoList).build();
    }

    @ApiOperation("评论审核")
    @PostMapping("/verFlag")
    public AjaxResult verFlag(Long id, Integer flag){
        EducationForumComment comment = commentService.getById(id);
        if (StringUtils.checkObjAllFieldsIsNull(comment)){
            return AjaxResult.error("评论不存在");
        }
        comment.setVerFlag(flag);
        boolean save = commentService.updateById(comment);
        return toAjax(save);
    }
}
